﻿
;(function($) {
    $.fn.DTabs = function(options) {
        $.fn.DTabs.defaults = {
            firstOn: 0,
            className: 'on',
			hoverClassName:'',
            eventName: 'all',         //click,mouserover,all 
            loadName: '加载中...',    //ajax等待字符串 
            fadeIn: 'normal',
			ifrBox:'normal',
            autoFade: false,
            autoFadeTime: 3
        };
        var opts = $.extend({},$.fn.DTabs.defaults, options);
		
        return this.each(function() {
            var target = $(this);
            var div; //所有的tabs显示体div 
            var tabs = target.find('.d-tabs-item'); //所有的tabs头部索引 
			if(opts.ifrBox!='normal'){
				div = opts.ifrBox; 
			}else{
				div = target.find(".d-tabs-view");	
			}
			var dindex=0;
			var index ;
			var timer=null;
			
            function Tabs(event,n) {
                if(!!opts.hoverClassName){
						$(this).removeClass(opts.hoverClassName);
				}
				if ($(this).hasClass(opts.className)) {
                    return false;
                }
				dindex=tabs.index($(this));
				if (opts.autoFade) {index=dindex}
				if(!n>0){
					window.clearInterval(timer);
				}
                tabsShow(div, $(this));
                return false;
            }
			function tabsShowIfr(div, li, index) {
				var kindex;
				if(typeof(index) == "number"){
					kindex=index;
				}else{
					kindex=dindex;
				}				
                div.attr("src",li.attr("href"));
				tabs.stop(true, true).removeClass(opts.className);
				$(tabs[kindex]).stop(true, true).addClass(opts.className);
            }
            function tabsShow(div, li, index) {
				if(opts.ifrBox!='normal'){
					tabsShowIfr(div, li, index);
					return;
				}
                div.stop(true, true).hide();
                //自动轮换用 
                if (typeof(index) == "number") {
                    if (li.attr("href")) ajax(div.eq(index), li);
                    $(div[index]).stop(true, true).fadeIn(opts.fadeIn);
                    tabs.stop(true, true).removeClass(opts.className);
                    $(tabs[index]).stop(true, true).addClass(opts.className);
                }
                //非自动轮换 
                else {
                    var tabBody = div.eq(dindex);
                    if (li.attr("href")) ajax(tabBody, li);
                    tabBody.stop(true, true).fadeIn(opts.fadeIn);
                    tabs.stop(true, true).removeClass(opts.className);
                    li.stop(true, true).addClass(opts.className);
                }
            }
            function ajax(div, li) {
                var href = li.attr("href");              
                var i = div; //当前div 
                if (href) { //如果ajax请求url不为空，只ajax一次 
                    i.html(opts.loadName);
                    $.ajax({
                        url: href,
                        cache: false,
                        success: function(html) {
                            i.html(html);
                        },
                        error: function() {
                            i.html('加载错误，请重试！');
                        }
                    });
                    li.removeAttr("href"); //只ajax一次 
                }
            }
            if (opts.autoFade) {
                index = opts.firstOn;
               timer=setInterval(function(){			
					index++;					
                    if (index >= div.length) {
                        index = 0;
                    }
                    tabsShow(div, $(this), index);
                },
                opts.autoFadeTime * 1000);
            }
			if(!!opts.hoverClassName){			
				tabs.hover(
					function(){
						if($(this).hasClass(opts.className)){return}
						$(this).addClass(opts.hoverClassName);
					},
					function(){
						if($(this).hasClass(opts.className)){return}
						$(this).removeClass(opts.hoverClassName);}
				);
			}			
            tabs.bind(opts.eventName == 'all' ? 'click mouseover': opts.eventName, Tabs) //绑定事件 
			.filter(':first').trigger(opts.eventName == 'all' ? 'click': opts.eventName,10); //自动触发事件 			
		});			
	}
})(jQuery);